package gwt.example.client;

import gwt.example.client.celllist.SimpleCellList;

import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Arrays;
import java.util.List;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellBrowser;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.TreeViewModel;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.logging.client.HasWidgetsLogHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Gwt_example implements EntryPoint {

	@Override
	public void onModuleLoad() {
		// celllistExample();
		// tableExample();
		// treeExample();
		// browserExample();
		// loggingExample();		
		
		simpleCellList();
	}

	public void simpleCellList(){
		SimpleCellList cellList = new SimpleCellList();		
		RootPanel.get("defaultContent").setVisible(false);
		RootPanel.get("mainContent").add(cellList, 0, 0);		
	}
	
	
	public void celllistExample() {
		final List<String> days = Arrays.asList("Sunday", "Monday", "Tuesday",
				"Wednesday", "Thursday", "Friday", "Saturday");
		TextCell textcell = new TextCell();
		CellList<String> cellList = new CellList<String>(textcell);
		cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		final SingleSelectionModel<String> singleSelection = new SingleSelectionModel<String>();
		cellList.setSelectionModel(singleSelection);
		singleSelection
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					@Override
					public void onSelectionChange(SelectionChangeEvent event) {
						String selected = singleSelection.getSelectedObject();
						if (selected != null)
							Window.alert("You selected " + selected);
					}
				});

		cellList.setRowCount(days.size());
		cellList.setRowData(days);
		RootPanel.get().add(cellList);
	}

	public void tableExample() {
		ProvidesKey<Contact> keyProvider = new ProvidesKey<Gwt_example.Contact>() {
			@Override
			public Object getKey(Contact item) {
				if (item == null)
					return null;
				return item.id;
			}
		};

		List<Contact> contacts = Arrays.asList(new Contact("Pham",
				"123 Fourth Road"), new Contact("Hai", "124 Fourth Road"),
				new Contact("Dang", "125 Fourth Road"), new Contact("John",
						"126 Fourth Road"), new Contact("Marry",
						"222 Lancer Lane"));
		CellTable<Contact> table = new CellTable<Contact>(keyProvider);
		TextColumn<Contact> nameColumn = new TextColumn<Gwt_example.Contact>() {
			@Override
			public String getValue(Contact object) {
				return object.name;
			}
		};
		TextColumn<Contact> addressColumn = new TextColumn<Gwt_example.Contact>() {
			@Override
			public String getValue(Contact object) {
				return object.address;
			}
		};
		table.addColumn(nameColumn, "Name");
		table.addColumn(addressColumn, "Address");

		table.setRowCount(contacts.size(), true);
		table.setRowData(contacts);

		RootPanel.get().add(table);

	}

	public void treeExample() {
		TreeViewModel treeview = new CustomTree();
		CellTree cellTree = new CellTree(treeview, "Item 1");
		RootPanel.get().add(cellTree);
	}

	public void browserExample() {
		TreeViewModel treeview = new CustomTree();
		CellBrowser cellbrowser = new CellBrowser.Builder<String>(treeview, "Item 1").build();
		RootPanel.get().add(cellbrowser);
	}

	public class CustomTree implements TreeViewModel {

		@Override
		public <T> NodeInfo<?> getNodeInfo(T value) {
			ListDataProvider<String> dataProvider = new ListDataProvider<String>();
			for (int i = 0; i < 2; i++) {
				dataProvider.getList().add(value + "." + i);
			}
			return new DefaultNodeInfo<String>(dataProvider, new TextCell());
		}

		@Override
		public boolean isLeaf(Object value) {
			return value.toString().length() > 10;
		}

	}

	private TabPanel tabPanel;
	/* Create Root Logger */
	private static Logger rootLogger = Logger.getLogger("");
	private VerticalPanel customLogArea;
	private void selectTab(String historyToken) {
		try {
			if (historyToken.substring(0, 9).equals("pageIndex")) {
				String tabIndexToken = historyToken.substring(9, 10);
				int tabIndex = Integer.parseInt(tabIndexToken);
				/* Select the specified tab panel */
				tabPanel.selectTab(tabIndex);
			} else {
				tabPanel.selectTab(0);
			}
		} catch (IndexOutOfBoundsException e) {
			tabPanel.selectTab(0);
		}
	}

	/**
	 * This is the entry point method.
	 */
	public void loggingExample() {
		/* create a tab panel to carry multiple pages */
		tabPanel = new TabPanel();

		/* create pages */
		HTML firstPage = new HTML("<h1>We are on first Page.</h1>");
		HTML secondPage = new HTML("<h1>We are on second Page.</h1>");
		HTML thirdPage = new HTML("<h1>We are on third Page.</h1>");

		String firstPageTitle = "First Page";
		String secondPageTitle = "Second Page";
		String thirdPageTitle = "Third Page";

		Hyperlink firstPageLink = new Hyperlink("1", "pageIndex0");
		Hyperlink secondPageLink = new Hyperlink("2", "pageIndex1");
		Hyperlink thirdPageLink = new Hyperlink("3", "pageIndex2");
		
		HorizontalPanel linksHPanel = new HorizontalPanel();
		linksHPanel.setSpacing(10);
		linksHPanel.add(firstPageLink);
		linksHPanel.add(secondPageLink);
		linksHPanel.add(thirdPageLink);

		/*
		 * If the application starts with no history token, redirect to a
		 * pageIndex0
		 */
		String initToken = History.getToken();

		if (initToken.length() == 0) {
			History.newItem("pageIndex0");
			initToken = "pageIndex0";
		}

		tabPanel.setWidth("400");
		/* add pages to tabPanel */
		tabPanel.add(firstPage, firstPageTitle);
		tabPanel.add(secondPage, secondPageTitle);
		tabPanel.add(thirdPage, thirdPageTitle);

		/*
		 * add value change handler to History this method will be called, when
		 * browser's Back button or Forward button are clicked. and URL of
		 * application changes.
		 */
		History.addValueChangeHandler(new ValueChangeHandler<String>() {
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				selectTab(event.getValue());
				rootLogger.log(Level.SEVERE,
						"pageIndex selected: " + event.getValue());
			}
		});

		selectTab(initToken);

		VerticalPanel vPanel = new VerticalPanel();

		vPanel.setSpacing(10);
		vPanel.add(tabPanel);
		vPanel.add(linksHPanel);

		customLogArea = new VerticalPanel();
		vPanel.add(customLogArea);

		/* an example of using own custom logging area. */
		rootLogger.addHandler(new HasWidgetsLogHandler(customLogArea));

		/* add controls to RootPanel */
		RootPanel.get().add(vPanel);
	}

	static class Contact {
		static int nextid = 1;
		final int id;
		final String address;
		final String name;

		public Contact(String name, String address) {
			this.id = nextid++;
			this.name = name;
			this.address = address;
		}
	}

}
